package com.order.manager.controller.Menu;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.order.manager.Model.MenuInfo;
import com.order.manager.Model.ShopInfo;
import com.order.manager.Model.other.RestBean;
import com.order.manager.service.impl.MenuInfoServiceImpl;
import com.order.manager.service.impl.ShopInfoServiceImpl;
import com.order.manager.util.JwtUtil;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("/api/menu")
public class MenuController {

    @Resource
    MenuInfoServiceImpl menuInfoService;

    @Resource
    ShopInfoServiceImpl shopInfoService;

    @Resource
    JwtUtil jwtUtil;

    @RequestMapping("/insert")
    public RestBean insertMenu(@RequestBody MenuInfo menuInfo){
        menuInfo.setMenuId(String.valueOf(UUID.randomUUID()));
        menuInfo.setInputTime(new Date());
        if (menuInfo.getMenuName().isEmpty())
            return RestBean.failure(403,"菜品名称不能为空");
        if (shopInfoService.getById(menuInfo.getShopId()) == null){
            return RestBean.failure(403,"fail auth ,商店有问题");
        }
        if (menuInfoService.save(menuInfo)){
            return RestBean.success(menuInfo);
        }
        return RestBean.failure(500,"服务器故障，请联系管理员");
    }

    @RequestMapping("/del/{menuId}")
    public RestBean delMenu(@PathVariable String menuId){
        MenuInfo menuInfo = menuInfoService.getById(menuId);
        if (menuInfo == null){
            return RestBean.failure(403,"没有该菜品");
        }
        return menuInfoService.removeById(menuInfo)?RestBean.success("删除成功"):
                RestBean.failure(500,"服务器故障，请联系管理员");
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody MenuInfo menuInfo,HttpServletRequest request){
        String uuid = jwtUtil.getJwtUsername(request);
        ShopInfo shopMasterId = shopInfoService.getOne(new QueryWrapper<ShopInfo>()
                .eq("shop_master_id", uuid)
        );
        if (shopMasterId == null)
            return RestBean.failure(403,"没有店铺信息");
        menuInfo.setShopId(shopMasterId.getShopId());
        MenuInfo menuInfoServiceOne = menuInfoService.getOne(new QueryWrapper<MenuInfo>()
                .eq("menu_id", menuInfo.getMenuId())
        );
        if (menuInfoServiceOne == null ){
            return RestBean.failure(403,"没有该菜品");
        } else{
            menuInfo.setShopId(menuInfoServiceOne.getShopId());
            menuInfo.setInputTime(menuInfoServiceOne.getInputTime());
            if (menuInfoService.updateById(menuInfo)){
             return RestBean.success(menuInfo);
         }
         return RestBean.failure(500,"服务器故障，请联系管理员");
        }
    }

    @RequestMapping("/select/{shopId}/{page}/{item}")
    public RestBean queryList(@PathVariable String shopId,@PathVariable int page
            ,@PathVariable int item,String menuName){
        return  RestBean.success(menuInfoService.queryByShop(shopId, menuName, page, item));
    }

    @RequestMapping("/shopSelect/{page}/{item}")
    public RestBean queryByShopList(@PathVariable int page
            , @PathVariable int item, String menuName, HttpServletRequest request){
        String uuid = jwtUtil.getJwtUsername(request);
        ShopInfo shopMasterId = shopInfoService.getOne(new QueryWrapper<ShopInfo>()
                .eq("shop_master_id", uuid)
        );
        if (shopMasterId == null){
            return RestBean.failure(403,"没有开店铺");
        }
        return  RestBean.success(menuInfoService.ShopQueryByShop(shopMasterId.getShopId(), menuName, page, item));
    }


    @RequestMapping("/get/{page}/{item}")
    public RestBean GetAllMenu(@PathVariable int page,@PathVariable int item,String  menuName){
     return RestBean.success(menuInfoService.queryByName(menuName, page, item));
    }


}
